package com.xiangyi.teachingsystem.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiangyi.teachingsystem.entity.dto.GroupDto;
import com.xiangyi.teachingsystem.entity.dto.UserGroupTaskCourseDto;
import com.xiangyi.teachingsystem.entity.po.UserGroup;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper

public interface UserGroupMapper extends BaseMapper<UserGroup> {

    @Select("SELECT g.id,g.task_id,g.group_name,u.`name`,g.leader_id,tr.grade,tr.`comment`,ts.commit_status,ts.summary\n" +
            "FROM user_group g\n" +
            "LEFT JOIN `user` u\n" +
            "ON g.leader_id=u.id\n" +
            "LEFT JOIN teacher_review tr\n" +
            "ON tr.group_id=g.id\n" +
            "LEFT JOIN task_sum ts\n" +
            "ON ts.group_id=g.id\n" +
            "WHERE g.task_id=#{taskId}")
    List<UserGroup> groupList(Integer taskId);

    @Select("SELECT g_info.group_id,g_info.task_id,g_info.group_name,g_info.leader_id,t_info.task_name,t_info.deadline,t_info.course_name,t_info.sign,t_info.course_id,t_info.teacher_id,t_info.teacher_name,u2.`name` leader_name\n" +
            "FROM\n" +
            "(SELECT gu.group_id,ug.task_id,ug.group_name,ug.leader_id\n" +
            "FROM group_user gu\n" +
            "LEFT JOIN user_group ug\n" +
            "ON gu.group_id=ug.id\n" +
            "WHERE gu.user_id=#{userId}) g_info\n" +
            "LEFT JOIN\n" +
            "(SELECT t.id task_id,t.task_name,t.deadline,c.course_name,c.sign,c.id course_id,uc.user_id teacher_id,u.`name` teacher_name\n" +
            "FROM task t\n" +
            "LEFT JOIN course c\n" +
            "ON t.course_id=c.id\n" +
            "LEFT JOIN user_course uc\n" +
            "ON uc.course_id=c.id\n" +
            "LEFT JOIN `user` u\n" +
            "ON u.id=uc.user_id) t_info\n" +
            "ON g_info.task_id=t_info.task_id\n" +
            "LEFT JOIN `user` u2\n" +
            "ON g_info.leader_id=u2.id")
    List<UserGroupTaskCourseDto> getGroupsByUserId(Integer userId);

    @Select("SELECT ug.id group_id,ug.group_name,ug.leader_id,(\n" +
            "SELECT u.`name`\n" +
            "FROM `user` u\n" +
            "WHERE u.id=ug.leader_id\n" +
            ") leader_name\n" +
            "FROM group_user gu\n" +
            "LEFT JOIN user_group ug\n" +
            "ON gu.group_id=ug.id\n" +
            "WHERE gu.user_id=#{userId}\n" +
            "AND ug.task_id=#{taskId}")
    List<GroupDto> listByUserAndTaskIds(Integer userId, Integer taskId);

    @Select("SELECT g.id,g.task_id,g.group_name,u.`name`,g.leader_id,tr.grade,tr.`comment`,ts.commit_status,ts.summary\n" +
            "FROM user_group g\n" +
            "LEFT JOIN `user` u\n" +
            "ON g.leader_id=u.id\n" +
            "LEFT JOIN teacher_review tr\n" +
            "ON tr.group_id=g.id\n" +
            "LEFT JOIN task_sum ts\n" +
            "ON ts.group_id=g.id\n" +
            "WHERE g.id=#{groupId}")
    UserGroup getInfoById(Integer groupId);
}
